
package ui.activity.poscontrol.adapter;

import android.content.Context;

import net.ck.hzq.R;
import net.ck.hzq.TobuyApplication;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import Utils.ToastUtil;
import base.BaseViewHolder;
import base.SuperBaseAdapter;
import event.PosControlEvent;
import ui.activity.poscontrol.beanmodel.PushBackPosBean;


/**
 *POS查询列表适配器
 * Created by chenjian on 19/2/27
 */
public class PushBackPosAdapter extends SuperBaseAdapter<PushBackPosBean.RecordsBean> {

    List<PushBackPosBean.RecordsBean> mData = new ArrayList<>();
    private List<String>  selectPosition = new ArrayList<>();//选择的条目
    private List<String> mSelectedDevices = new ArrayList<>();
    Context mContext;
    private int piliangstartposition = -1;//批量开始坐标
    private int piliangendposition = -1;//批量结束坐标
    private String mposition ="";
    public PushBackPosAdapter(Context context, List<PushBackPosBean.RecordsBean> data,String position) {
        super(context, data);
        mContext = context;
        mData = data;
        mposition = position;
    }
    public void refresh() {
        selectPosition.clear();
        mSelectedDevices.clear();
    }

    @Override
    protected void convert(BaseViewHolder holder, final PushBackPosBean.RecordsBean item, final int position) {
        holder.setText(R.id.index, (position+1)+"")
                .setText(R.id.return_usedate, item.getRemainingDay()+"")
                .setText(R.id.device_no, item.getSn());//终端编号
        if(item.getRemainingDay()<=10){
            holder.setTextColorRes(R.id.return_usedate,R.color.red_fe3333);
        }else {
            holder.setTextColorRes(R.id.return_usedate,R.color.tc_666666);
        }
        if (selectPosition.indexOf(position+"") > -1) {
            holder.getView(R.id.cb).setBackgroundResource(R.mipmap.checkedbox);
        } else {
            holder.getView(R.id.cb).setBackgroundResource(R.mipmap.checkbox);
        }
        holder.getView(R.id.layout4).setOnClickListener(v -> {


            if(TobuyApplication.piliangPosition.contains(mposition)){
                if(piliangstartposition==-1&&piliangendposition == -1){
                    piliangstartposition = position;
                    if (mSelectedDevices.indexOf(item.getSn()) < 0) {
                        mSelectedDevices.add(item.getSn());
                        selectPosition.add(position + "");
                        item.setIsselect(true);
                        holder.getView(R.id.cb).setBackgroundResource(R.mipmap.checkedbox);

                    } else {
                        mSelectedDevices.remove(item.getSn());
                        selectPosition.remove(position + "");
                        item.setIsselect(false);
                        holder.getView(R.id.cb).setBackgroundResource(R.mipmap.checkbox);
                    }
                    ToastUtil.show(mContext,"批量起点已选择");
                }else  if(piliangstartposition!=-1&&piliangendposition == -1) {
                    ToastUtil.show(mContext,"批量终点已选择，已完成批量选择！");
                    piliangendposition = position;
                    if(piliangstartposition!=piliangendposition) {
                        if(piliangstartposition<piliangendposition){
                            for (int i = piliangstartposition + 1; i < piliangendposition + 1; i++) {
                                if (mSelectedDevices.indexOf(mData.get(i).getSn()) < 0) {
                                    mData.get(i).setIsselect(true);
                                    mSelectedDevices.add(mData.get(i).getSn());
                                    selectPosition.add(i + "");
                                } else {
                                    mSelectedDevices.remove(mData.get(i).getSn());
                                    selectPosition.remove(i + "");
                                    mData.get(i).setIsselect(false);
                                }
                            }
                        }else{
                            for (int i = piliangendposition ; i < piliangstartposition; i++) {
                                if (mSelectedDevices.indexOf(mData.get(i).getSn()) < 0) {
                                    mData.get(i).setIsselect(true);
                                    mSelectedDevices.add(mData.get(i).getSn());
                                    selectPosition.add(i + "");
                                } else {
                                    mSelectedDevices.remove(mData.get(i).getSn());
                                    selectPosition.remove(i + "");
                                    mData.get(i).setIsselect(false);
                                }
                            }
                        }

                        piliangstartposition = -1;
                        piliangendposition = -1;
                        notifyDataSetChanged();
                    }else{
                        piliangstartposition = -1;
                        piliangendposition = -1;

                        if (mSelectedDevices.indexOf(item.getSn()) < 0) {
                            mSelectedDevices.add(item.getSn());
                            mData.get(position).setIsselect(true);
                            selectPosition.add(position + "");
                            holder.getView(R.id.cb).setBackgroundResource(R.mipmap.checkedbox);

                        } else {
                            mSelectedDevices.remove(item.getSn());
                            mData.get(position).setIsselect(false);
                            selectPosition.remove(position + "");
                            holder.getView(R.id.cb).setBackgroundResource(R.mipmap.checkbox);
                        }
                    }
                }
            }else {
                piliangstartposition = -1;
                piliangendposition = -1;
                if (mSelectedDevices.indexOf(item.getSn()) < 0) {
                    mSelectedDevices.add(item.getSn());
                    mData.get(position).setIsselect(true);
                    selectPosition.add(position + "");
                    holder.getView(R.id.cb).setBackgroundResource(R.mipmap.checkedbox);

                } else {
                    mSelectedDevices.remove(item.getSn());
                    selectPosition.remove(position + "");
                    mData.get(position).setIsselect(false);
                    holder.getView(R.id.cb).setBackgroundResource(R.mipmap.checkbox);
                }
            }
            EventBus.getDefault().post(new PosControlEvent(mData));
        });

    }



    @Override
    protected int getItemViewLayoutId(int position, PushBackPosBean.RecordsBean item) {
        return R.layout.item_pushbackpos_list;
    }

}
